<?php

class CategoryForm extends TPage
{
	const AR = "CategoryRecord";
	const NO_IMAGE = "noimage.png";
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " Administration";
		if (!$this->IsPostBack)
		{
			// fill parent selector combobox
			$this->cboParentSelector->DataSource = Prado::createComponent(self::AR)->getAllParent();
			$this->cboParentSelector->DataBind();
			$activeRecord = $this->getItem();
			if ($activeRecord && $activeRecord->ID > 0)
			{
				// Populates the input controls with the existing post data
				$this->lblHeader->Text = "Update category: ".$activeRecord->Name;
				$this->txtName->Text = $activeRecord->Name;
				$this->radPublish->SelectedValue = $activeRecord->IsPublished;
				$this->cboParentSelector->SelectedValue = $activeRecord->ParentID;
			}
			else
			{
				$this->lblHeader->Text = "Add new category";
			}
		}
	}

	protected function getItem()
	{
		if ($this->Request->Contains("id") && $this->Request->Contains("alias"))
		{
			// use Active Record to look for the specified post ID
			$activeRecord = Prado::createComponent(self::AR)->finder()->findBycat_idAndcat_alias(TPropertyValue::ensureInteger($this->Request['id']), $this->Request['alias']);
			if($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","category");
				$this->mainBox->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			return Prado::createComponent(self::AR);
		}
	}
	
	private function bindItem()
	{
		$activeRecord = $this->getItem();
		
		$activeRecord->Name = $this->txtName->SafeText;
		$activeRecord->Alias = "";
		$activeRecord->IsPublished = $this->radPublish->SelectedValue;
		$activeRecord->ParentID = $this->cboParentSelector->SelectedValue;
		
		return $activeRecord;
	}

	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$activeRecord = $this->bindItem();
			try
			{
				$action = ($activeRecord->ID>0 ? "update-success" : "add-success");
				$msg = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_SUCCESS" : "ADD_SUCCESS"),"Category",$activeRecord->Name);
				$activeRecord->save();
				//$this->Response->redirect($this->Service->ConstructUrl("admincp.CategoryManager",array("action"=>$action, "msg"=>$msg)));
				if (strlen($this->Request["refUrl"])>0)
					$url = urldecode($this->Request["refUrl"])."&action=$action&msg=$msg";
				else  $url = $this->Service->ConstructUrl("admin.ManageCategories",array("action"=>$action, "msg"=>$msg));
				$this->Response->redirect($url);
			}
			catch(TException $e)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_FAILED" : "ADD_FAILED"),"Category",$activeRecord->Name);
			}
		}
	}
}

?>